package com.example.extendanimationdemo;

import java.util.Timer;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
	private TextView tv;
	private Button btn1;
	private Button btn2;
	private LinearLayout.LayoutParams layoutParams;
	private int ll1MeasureHeight;
	private int i;
	private Timer timer;
	private LinearLayout ll1;
	private LinearLayout ll2;
	private Button btn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		initAction();

	}

	private void initAction() {
		// TODO Auto-generated method stub
		btn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(ll1.isShown()){
					return;
				}
				ExpandAnimation1 ll1EexpandAnimation1 = new ExpandAnimation1(ll1, View.VISIBLE);
				ll1EexpandAnimation1.setDuration(500);
				ll1.startAnimation(ll1EexpandAnimation1);
				ExpandAnimation1 ll2ExpandAnimation1 = new ExpandAnimation1(ll2, View.GONE);
				ll2ExpandAnimation1.setDuration(500);
				ll2.startAnimation(ll2ExpandAnimation1);

			}
		});
		btn2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(ll2.isShown()){
					return;
				}
				ExpandAnimation1 ll1EexpandAnimation1 = new ExpandAnimation1(ll1, View.GONE);
				ll1EexpandAnimation1.setDuration(500);
				ll1.startAnimation(ll1EexpandAnimation1);

				ExpandAnimation1 ll2ExpandAnimation1 = new ExpandAnimation1(ll2, View.VISIBLE);
				ll2ExpandAnimation1.setDuration(500);
				ll2.startAnimation(ll2ExpandAnimation1);
			}
		});
	}

	private void initView() {
		ll1 = (LinearLayout) findViewById(R.id.ll1);
		ll2 = (LinearLayout) findViewById(R.id.ll2);
		btn1 = (Button) findViewById(R.id.btn1);
		btn2 = (Button) findViewById(R.id.btn2);
		btn = (Button) findViewById(R.id.btn);
		// btn动画
		/*btn.measure(0, 0);
		int measuredHeight2 = btn.getMeasuredHeight();
		int top = btn.getTop();
		int left = btn.getLeft();
		System.out.println("top:" + top + "left:" + left);
		TranslateAnimation trAnimation = new TranslateAnimation(left, left, top + 2 * measuredHeight2, top);
		AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
		alphaAnimation.setDuration(500);
		alphaAnimation.setInterpolator(new AccelerateInterpolator());
		trAnimation.setDuration(500);
		trAnimation.setInterpolator(new AccelerateInterpolator());
		AnimationSet set = new AnimationSet(true);
		set.addAnimation(alphaAnimation);
		set.addAnimation(trAnimation);
		btn.startAnimation(set);*/
		// 自定义动画
		ExpandAnimation expandAnimation = new ExpandAnimation(btn);
		expandAnimation.setDuration(500);
		expandAnimation.setInterpolator(new AccelerateInterpolator());
		btn.startAnimation(expandAnimation);
		//
		int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
		int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
		int w1 = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.AT_MOST);
		int h1 = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.AT_MOST);
		int w2 = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.EXACTLY);
		int h2 = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.EXACTLY);
		System.out.println("w:" + w + " " + "h:" + h);
		System.out.println("w1:" + w1 + " " + "h1:" + h1);
		System.out.println("w2:" + w2 + " " + "h2:" + h2);
		System.out.println("Integer.MAX_VALUE:" + Integer.MAX_VALUE);
		System.out.println("Integer.MIN_VALUE:" + Integer.MIN_VALUE);
		((View) ll1.getParent()).measure(0, 0);
		ll1MeasureHeight = ll1.getMeasuredHeight();
		layoutParams = (android.widget.LinearLayout.LayoutParams) ll1.getLayoutParams();
		layoutParams.bottomMargin = -ll1MeasureHeight;
		ll1.setVisibility(8);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}
